package com.red.wood.module.eval.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.eval.service.EvaluationInfoService;

@Controller
@RequestMapping("/evaluationInfo")
public class EvaluationInfoController {
	
	private static final Log log = LogFactory.getLog(EvaluationInfoController.class);
	
	@Autowired
	private EvaluationInfoService service;
	
	/**
	 * 信息评测
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("evaluationInfo");
//		mv.addObject("ircsId", CommonUtils.valueOf(request.getParameter("ircsId")));
		return mv;
	}
	
	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getEvaluationInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getEvaluationInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			//			查询条件：
//			企业许可证号：（文本框输入：可支持模糊查询）
//			申请业务（下拉框选择，包括 全部、IDC+ISP、ISP、IDC(不含互联网资源协作)、CDN、IDC(仅限互联网资源协作) ）
//			企业名称（文本框输入：可支持模糊查询）
//			表单编号（文本框输入：可支持模糊查询）
//			表单状态（下拉框选择：包括 全部、评测通过、评测未通过）
//			注册地（下拉框选择：包括 全部+全国31个省市自治区，）
			param.put("corporateNo", CommonUtils.valueOf(request.getParameter("corporateNo"))); //企业许可证号
			param.put("applyBusiness", CommonUtils.valueOf(request.getParameter("applyBusiness"))); // 申请业务
			param.put("corporateName", CommonUtils.valueOf(request.getParameter("corporateName"))); //企业名称
			param.put("formNum", CommonUtils.valueOf(request.getParameter("formNum"))); // 表单编号
			param.put("formState", CommonUtils.valueOf(request.getParameter("formState"))); // 表单状态
			param.put("registerLocal", CommonUtils.valueOf(request.getParameter("registerLocal"))); // 注册地
			
			int pageSize = Integer.parseInt(CommonUtils.valueOf(request.getParameter("pageSize")));
			String currentPage = CommonUtils.valueOf(request.getParameter("currentPage"));
			if(StringUtils.isBlank(currentPage)) {
				currentPage = "1";
			}
			Page page = Page.newBuilder(Integer.parseInt(currentPage),pageSize,null);  
			param.put("page",page);
			
			Map<String,Object> rs = service.getPageData(param);
			page.setTotalRecord(Integer.parseInt(CommonUtils.valueOf(rs.get("totalCount"))));
			List<Map<String,Object>> list = (List<Map<String, Object>>) rs.get("pageData");
			
			json.put("flag", true);
			json.put("datas",list);
			json.put("totalPages",page.getTotalPage());
			json.put("totalData", page.getTotalRecord());
			json.put("currentPage", currentPage);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}
		return json.toString();
	}
}
